<template>
  <div id="main">

    <h2>系统用户管理</h2>

    <el-button type="primary" style="margin:20px" @click="handleOpen('添加管理员用户',1)">添加用户</el-button>

    <div id="search-card">
      <div id="search-content">
        <el-form v-model="search" :inline="true">

          <el-form-item label="昵称">
            <el-input v-model="search.username" />
          </el-form-item>

          <el-form-item label="所属机构">
            <!-- <el-input v-model="search.phone" /> -->
            <el-select v-model="search.orgId" placeholder="请选择">
              <el-option
                v-for="o in org"
                :key="o.orgId"
                :value="o.orgId"
                :label="o.orgName"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="所属用户组">
            <!-- <el-input v-model="search.idCard" /> -->
            <el-select v-model="search.userGroup" placeholder="请选择">
              <el-option key="1" label="系统管理员" value="1" />
              <el-option key="2" label="推荐人员" value="2" />
              <el-option key="3" label="机构管理员" value="3" />
              <el-option key="4" label="分支机构管理员" value="4" />
            </el-select>
          </el-form-item>

          <!-- <el-form-item label="活动地点">
            <el-input v-model="search.actAddress" />
          </el-form-item> -->

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="conditionQuery">查找</el-button>
          </el-form-item>

          <el-form-item label="模糊查询:">
            <el-input v-model="mohu" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="mohuQuery">查找</el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>

    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >

        <el-table-column
          label="序号"
          type="index"
          width="80"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="username"
          label="昵称"
        />

        <el-table-column
          align="center"
          prop="userGroup"
          label="所属用户组"
        >
          <template slot-scope="Scope">
            <span v-if="Scope.row.userGroup==1">系统管理员</span>
            <span v-if="Scope.row.userGroup==2">推荐人员</span>
            <span v-if="Scope.row.userGroup==3">机构管理员</span>
            <span v-if="Scope.row.userGroup==4">分支机构管理员</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpen('修改管理员用户',2,Scope.row.userId)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="handleDelete(Scope.row.userId,Scope.row.username)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog title="" :close-on-click-modal="false" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form ref="consoleUser" :model="consoleUser" label-position="top" :rules="rules">

        <el-form-item prop="username" label="昵称" :label-width="formLabelWidth">
          <el-input
            v-model="consoleUser.username"
            autocomplete="off"
            maxlength="20"
            show-word-limit
          />
        </el-form-item>

        <el-form-item prop="password" label="密码" :label-width="formLabelWidth">
          <el-input v-model="consoleUser.password" autocomplete="off" type="password" />
        </el-form-item>

        <el-form-item prop="userGroup" label="用户类别" :label-width="formLabelWidth">
          <el-select v-model="consoleUser.userGroup" placeholder="请选择">
            <el-option key="1" label="系统管理员" value="1" />
            <el-option key="2" label="推荐人员" value="2" />
            <el-option key="3" label="机构管理员" value="3" />
            <el-option key="4" label="分支机构管理员" value="4" />
          </el-select>
        </el-form-item>

        <el-form-item label="所属机构" :label-width="formLabelWidth">
          <el-select v-model="consoleUser.orgId" placeholder="机构管理员选择，其他管理员请选无">
            <el-option key="-1" label="无" value="-1" />
            <el-option
              v-for="o in org"
              :key="o.orgId"
              :value="o.orgId"
              :label="o.orgName"
            />
          </el-select>
        </el-form-item>

        <el-form-item prop="realname" label="账号所属人" :label-width="formLabelWidth">
          <el-input
            v-model="consoleUser.realname"
            autocomplete="off"
            maxlength="20"
            show-word-limit
          />
        </el-form-item>

        <el-form-item prop="userPhone" label="手机号" :label-width="formLabelWidth">
          <el-input v-model="consoleUser.userPhone" type="number" maxlength="11" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>
        <el-button v-if="editType==2" type="primary" @click="handleUpdate('consoleUser')">修 改</el-button>
        <el-button v-if="editType==1" type="primary" @click="handleAddUser('consoleUser')">添 加</el-button>
      </div>
    </el-dialog>

  </div>

</template>
<script>
import { getAllConsoleUserList, getConsoleUserById, addUser, deleteUser, updateUser, conditionSearch, mohuSearch } from '@/api/consoleUser'
import { getAllOrg } from '@/api/org'
export default {

  data() {
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      listLoading: false,
      dialogFormVisible: false,
      pwdFormVisible: false,
      org: {},
      editType: 1, // 1 添加 2 修改
      search: {},
      mohu: '',
      consoleUser: {},
      formLabelWidth: '200px',
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }, { min: 8, max: 12, message: '密码建议在8~12位', trigger: 'blur' }],
        userGroup: [{ required: true, message: '请选择用户类别' }]
      }
    }
  },
  created() {
    this.fetchData()
    this.getAllOrg()
  },
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },

    fetchData() {
      this.listLoading = true
      getAllConsoleUserList().then((data) => {
        this.list = data.obj
        this.listLoading = false
      })
    },
    handleDelete(id, username) {
      this.$confirm('确认删除该用户？昵称为' + username).then(() => deleteUser(id).then((data) => {
        this.$notify({
          title: '操作成功',
          type: 'success'
        })
        const totalPage = Math.ceil((this.list.length - 1) / this.pagesize)
        const page = this.currentPage > totalPage ? totalPage : this.currentPage
        this.currentPage = this.currentPage < 1 ? 1 : page
        this.fetchData()
      }))
    },
    handleAddUser(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addUser(this.consoleUser).then((resp) => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.consoleUser = {}
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleOpen(title, type, id) {
      this.dialogFormVisible = true
      this.title = title
      this.editType = type
      if (type === 2) {
        getConsoleUserById(id).then((resp) => {
          this.consoleUser = resp.obj
          this.consoleUser.userGroup = String(this.consoleUser.userGroup)
          if (this.consoleUser.orgId === -1) {
            this.consoleUser.orgId = '-1'
          }
        })
      }
    },
    handleClose() {
      this.dialogFormVisible = false
      this.consoleUser = {}
    },
    handleUpdate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateUser(this.consoleUser).then((resp) => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    getAllOrg() {
      getAllOrg().then((resp) => {
        this.org = resp.obj
      })
    },
    conditionQuery() {
      console.log(this.search)
      conditionSearch(this.search).then((data) => {
        this.list = data.obj
        this.search = {}
        this.currentPage = 1
      })
    },
    mohuQuery() {
      console.log(this.mohu)
      mohuSearch(this.mohu).then((data) => {
        this.list = data.obj
        this.currentPage = 1
      })
    }

  }

}
</script>

<style scoped>
     #main{
        margin-left: 5px;
        margin-right: 5px;
    }

    #table-area{

        margin-left: 10px;
        margin-right: 10px;
    }

</style>
